package com.mingliqiye.minecraft.enchantment.conflict.fabric;

import com.mingliqiye.minecraft.enchantment.conflict.common.ConfigPair;
import com.mingliqiye.minecraft.enchantment.conflict.common.MinecraftMod;
import com.mingliqiye.minecraft.enchantment.conflict.common.WebConfigCoreMeMethod;
import com.mingliqiye.minecraft.enchantment.conflict.common.enchantment.EnchantmentEntity;
import com.mingliqiye.minecraft.enchantment.conflict.fabric.network.ConfigPayload;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.enchantment.Enchantment;

public class FabricWebConfigCoreMeMethod implements WebConfigCoreMeMethod {

    @Override
    public void reloadConfig() {
    }

    @Override
    public Map<String, ConfigPair> getDefConfig() {
        return FabricMod.CONFIGMANAGER.getDefConfig().getConfig();
    }

    @Override
    public Map<String, ConfigPair> getConfig() {
        return FabricMod.CONFIGMANAGER.getConfig();
    }

    @Override
    public String getGameVersions() {
        Optional<ModContainer> minecraftContainer = FabricLoader.getInstance().getModContainer("minecraft");
        if (minecraftContainer.isPresent()) {
            return minecraftContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getModVersions() {
        Optional<ModContainer> EDNCContainer = FabricLoader.getInstance().getModContainer(MinecraftMod.MOD_ID);
        if (EDNCContainer.isPresent()) {
            return EDNCContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getLoaderVersions() {
        Optional<ModContainer> loaderContainer = FabricLoader.getInstance().getModContainer("fabricloader");
        if (loaderContainer.isPresent()) {
            return loaderContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getLoaderName() {
        return "Fabric";
    }

    @Override
    public List<EnchantmentEntity> getAllEnchantments() {

        Registry<Enchantment> enchantments = FabricMod.SERVER.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
        return enchantments.stream().map((a) -> {
            var sid = enchantments.getKey(a);
            return new EnchantmentEntity(sid.getNamespace() + ":" + sid.getPath(), null, a.description().getString(),
                    enchantments.getId(a));
        }).toList();
    }

    @Override
    public boolean getTest(Integer fid, Integer sid) {
        Registry<Enchantment> enchantments = FabricMod.SERVER.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
        var f = enchantments.asHolderIdMap().byId(fid);
        var s = enchantments.asHolderIdMap().byId(sid);
        return Enchantment.areCompatible(f, s);
    }

    @Override
    public void saveConfig(Map<String, ConfigPair> configPairMap) {
        FabricMod.CONFIGMANAGER.setConfig(configPairMap);
        FabricMod.CONFIGMANAGER.save();
        ConfigPayload.sendConfigToAllClient();
    }
}
